package com.alphacsp.theedge2012.ui.activities;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.TextView;
import com.alphacsp.theedge2012.R;
import com.alphacsp.theedge2012.data.database.DataAccessor;

import java.util.Calendar;
import java.util.Date;

public class HomeActivity extends Activity {

    private DataAccessor dataAccessor;
    TextView textView;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        //todo force update on first run
        super.onCreate(savedInstanceState);
        dataAccessor = DataAccessor.getSingleton(this);
        setContentView(R.layout.home_activity);
        new RefreshTask().execute();
        textView = (TextView) findViewById(R.id.now_playing_title);
        initNowPlayingView();
    }

    @Override
    protected void onPause() {
        super.onPause();
        dataAccessor.closeDatabase();
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public void onSpeakersClick(View v) {
        startActivity(new Intent(HomeActivity.this, SpeakersActivity.class));
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public void onSessionsClick(View v) {
        startActivity(new Intent(HomeActivity.this, SessionsActivity.class));
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public void onScheduleClick(View v) {
        startActivity(new Intent(HomeActivity.this, ScheduleActivity.class));
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public void onRefreshClick(View v) {
        new RefreshTask().execute();
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public void onAboutClick(View v) {
        startActivity(new Intent(HomeActivity.this, AboutActivity.class));
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public void onSearchClick(View v) {
        onSearchRequested();
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public void onMapClick(View v) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=Avenue+Festivities+and+Congress+Center+,Israel"));
        startActivity(intent);
    }

    private void setRefreshBar(boolean isUpdating) {
        final View progressBar = findViewById(R.id.title_refresh_progress);
        final View refreshButton = findViewById(R.id.refresh_button);

        if (isUpdating) {
            refreshButton.setVisibility(View.GONE);
            progressBar.setVisibility(View.VISIBLE);
        } else {
            refreshButton.setVisibility(View.VISIBLE);
            progressBar.setVisibility(View.GONE);
        }
    }

    private void initNowPlayingView() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.ZONE_OFFSET, 2);
        calendar.set(Calendar.MONTH, Calendar.MARCH);
        calendar.set(Calendar.DAY_OF_MONTH, 29);
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        final Date theEdgeDate = calendar.getTime();

        new CountDownTimer(theEdgeDate.getTime() - System.currentTimeMillis(), 1000) {
            @Override
            public void onTick(long l) {
                l = l / 1000;
                final int secs = (int) (l % 86400);
                final int days = (int) (l / 86400);
                final String str = getResources().getQuantityString(R.plurals.now_playing_countdown, days, days, DateUtils.formatElapsedTime(secs));
                textView.setText(str);
            }

            @Override
            public void onFinish() {
                textView.setText("Enjoy TheEdge 2012!");
            }
        }.start();

    }

    private class RefreshTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            setRefreshBar(true);
        }

        @Override
        protected Void doInBackground(Void... voids) {
            dataAccessor.forceUpdate();
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            setRefreshBar(false);
        }
    }


}
